/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.filedownload.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.filedownload.domain.UipFileDownloads;
import com.jeeplus.filedownload.service.dto.UipFileDownloadsDTO;
import com.jeeplus.filedownload.service.mapstruct.UipFileDownloadsWrapper;
import com.jeeplus.filedownload.service.UipFileDownloadsService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 文件下载Controller
 * @author lll
 * @version 2024-09-03
 */

@Api(tags ="文件下载")
@RestController
@RequestMapping(value = "/filedownload/uipFileDownloads")
public class UipFileDownloadsController {

	@Autowired
	private UipFileDownloadsService uipFileDownloadsService;

	@Autowired
	private UipFileDownloadsWrapper uipFileDownloadsWrapper;

	/**
	 * 文件下载列表数据
	 */
	@ApiLog("查询文件下载列表数据")
	@ApiOperation(value = "查询文件下载列表数据")
	@PreAuthorize("hasAuthority('filedownload:uipFileDownloads:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<UipFileDownloadsDTO>> list(UipFileDownloadsDTO uipFileDownloadsDTO, Page<UipFileDownloads> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (uipFileDownloadsDTO, UipFileDownloadsDTO.class);
		IPage<UipFileDownloadsDTO> result = uipFileDownloadsWrapper.toDTO ( uipFileDownloadsService.page (page, queryWrapper) );
		return ResponseEntity.ok (result);
	}


	/**
	 * 根据Id获取文件下载数据
	 */
	@ApiLog("根据Id获取文件下载数据")
	@ApiOperation(value = "根据Id获取文件下载数据")
	@PreAuthorize("hasAnyAuthority('filedownload:uipFileDownloads:view','filedownload:uipFileDownloads:add','filedownload:uipFileDownloads:edit')")
	@GetMapping("queryById")
	public ResponseEntity<UipFileDownloadsDTO> queryById(String id) {
		return ResponseEntity.ok ( uipFileDownloadsWrapper.toDTO ( uipFileDownloadsService.getById ( id ) ) );
	}

	/**
	 * 保存文件下载
	 */
	@ApiLog("保存文件下载")
	@ApiOperation(value = "保存文件下载")
	@PreAuthorize("hasAnyAuthority('filedownload:uipFileDownloads:add','filedownload:uipFileDownloads:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody UipFileDownloadsDTO uipFileDownloadsDTO) {
		//新增或编辑表单保存
		uipFileDownloadsService.saveOrUpdate (uipFileDownloadsWrapper.toEntity (uipFileDownloadsDTO));
        return ResponseEntity.ok ( "保存文件下载成功" );
	}


	/**
	 * 删除文件下载
	 */
	@ApiLog("删除文件下载")
	@ApiOperation(value = "删除文件下载")
	@PreAuthorize("hasAuthority('filedownload:uipFileDownloads:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        uipFileDownloadsService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除文件下载成功" );
	}
	
    /**
     * 导出文件下载数据
     *
     * @param uipFileDownloadsDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出文件下载数据")
    @PreAuthorize("hasAnyAuthority('filedownload:uipFileDownloads:export')")
    @GetMapping("export")
    public void exportFile(UipFileDownloadsDTO uipFileDownloadsDTO, Page <UipFileDownloads> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (uipFileDownloadsDTO, UipFileDownloadsDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List < UipFileDownloads> result = uipFileDownloadsService.page ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( uipFileDownloadsService, uipFileDownloadsWrapper ).exportExcel ( result,  options.getSheetName ( ), UipFileDownloadsDTO.class, fileName,options.getExportFields (), response );
    }

    /**
     * 导入文件下载数据
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('filedownload:uipFileDownloads:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( uipFileDownloadsService, uipFileDownloadsWrapper ).importExcel ( file, UipFileDownloadsDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入文件下载数据模板
     *
     * @param response
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('filedownload:uipFileDownloads:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "文件下载数据导入模板.xlsx";
        List<UipFileDownloadsDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( uipFileDownloadsService, uipFileDownloadsWrapper ).exportExcel ( list,  "文件下载数据", UipFileDownloadsDTO.class, fileName, null, response );
    }


}
